#ifndef ImageView_h
#define ImageView_h

#include <QWidget>
#include <QImageReader>
#include <QMutex>
#include <QThread>
#include <QScrollArea>

class ImageThread : public QThread
{
    Q_OBJECT
public:
    ImageThread(QObject *parent = nullptr):QThread(parent) {}
    virtual ~ImageThread() {}

    void start(const QString &path, QSize size);
    Q_SIGNAL void sigFinsihed(QPixmap pixmap);

private:
    void setNext(const QString &path, QSize size){
        _pathNext = path;
        _sizeNext = size;
        _hasNext = true;
    }
    bool turnToNext(){
        if(!_hasNext){
            return false;
        }
        _path = _pathNext;
        _size = _sizeNext;
        _hasNext = false;
        return true;
    }
    void run();

private:
    QString _path;
    QSize _size;

    bool _hasNext;
    QString _pathNext;
    QSize _sizeNext;
};

class ImageView : public QWidget
{
    Q_OBJECT
public:
    ImageView(QWidget *parent = nullptr);
    virtual ~ImageView();

    void setBorderWidth(int width){_borderWidth = width;}
    void reset(const QString &path, bool keepSize = false);
    void previewEnabled(bool enable){_previewEnabled = enable;}
    QString path(){return _path;}

protected:
    void resizeEvent(QResizeEvent *event);
    void paintEvent(QPaintEvent *event);
    void mouseDoubleClickEvent(QMouseEvent *event);

private:
    Q_SLOT void imageThread_onFinished(QPixmap pixmap);

private:
    QString _path;
    bool _keepSize;
    bool _previewEnabled;

    QPixmap _pixmap;
    ImageThread _imageThread;
    int _borderWidth;

    QScrollArea *_scrollArea;
};

#endif
